// 引入
import {checkLoginReq} from '@/api/user'

const state = {
  token: localStorage.getItem('t_k'),
  role: localStorage.getItem('role'),
}

const mutations = {
  SET_TOKEN(state, data) {
    // 存vuex
    state.token = data
    // 存本地
    localStorage.setItem('t_k', data)
  },
  SET_ROLE(state, data) {
    state.role = data
    // 存本地
    localStorage.setItem('role', data)
  },
}

const actions = {
  A_login({commit}, data) {
    return new Promise(async (resolve, reject) => {
      let res = await checkLoginReq(data)
      let {code, msg, token, role} = res.data
      if (code === 0) {
        commit('SET_TOKEN', 'Bearer ' + token)
        commit('SET_ROLE', role)
        resolve()
      }
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
